package com.zy.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class ScreenChangeUtils {

    public static void screenOutOrInAnimator(View childContainer, List<PointNode> pointNode, boolean out, ScreenChangeListener listener) {
        List<Animator> animatorList = new ArrayList<>();
        for (PointNode node : pointNode) {
            Animator pointAnimator;
            if (out) {
                pointAnimator = AnimatorUtils.getMove(node.origin, node.target);
            } else {
                pointAnimator = AnimatorUtils.getReset(node.origin);
            }
            animatorList.add(pointAnimator);
        }

        Animator animator = AnimatorUtils.getTogetherStart(animatorList);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation, boolean isReverse) {
                super.onAnimationStart(animation, isReverse);
                if (!out) {
                    childContainer.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation, boolean isReverse) {
                super.onAnimationEnd(animation, isReverse);
                if (out) {
                    childContainer.setVisibility(View.VISIBLE);
                }

                listener.screenEnd();
            }
        });
        animator.start();
    }

    public static class PointNode {
        View origin;
        View target;

        public PointNode(View origin, View target) {
            this.origin = origin;
            this.target = target;
        }
    }

    public interface ScreenChangeListener {
        void screenEnd();
    }
}
